package com.sourcestruct.linearlist;

import lombok.ToString;

/**
 * @author: ZhouBert
 * @date: 2019/6/8
 * @description:	链式栈（不限制容量）
 * 链式栈的出，入栈通过对链表进行头删和头插来实现
 */
@ToString
public class LinkedStack<T> {
	/**
	 * 存储数据的单链表(似乎只要一个 LinkedList 就够了)
	 */
	///private LinkedList linkedList;
	/**
	 * 栈顶节点(根据链表的顺序遍历特性，应该是头结点)
	 */
	private LinkedList<T> curr;

	/**
	 * 让指针指向栈顶
	 */
	public LinkedStack() {
	}

	/**
	 * 入栈
	 * @param obj
	 */
	public void push(T obj) {
		if (curr == null) {
			curr = new LinkedList(obj);
		} else {
			LinkedList node = new LinkedList(obj, curr);
			curr = node;
		}
	}

	/**
	 * 出栈
	 * @return
	 */
	public T pop() {
		if (curr == null) {
			return null;
		}
		///需要定义变量 node 带 泛型
		LinkedList<T> node = curr;
		curr = curr.getNext();
		node.setNext(null);
		return node.getData();
	}

	public static void main(String[] args) {
		LinkedStack<Integer> stack = new LinkedStack();
		stack.push(1);
		stack.push(2);
		stack.push(20);
		System.out.println(stack.toString());
		stack.pop();
		System.out.println(stack.toString());
	}
}
